//
//  NavCell.swift
//  Automobile
//
//  Created by erkebulan elzhan on 7/25/20.
//  Copyright © 2020 erkebulan elzhan. All rights reserved.
//

import UIKit
import SnapKit


final class NavItemView: UIView {
    
    private lazy var iconImg: UIImageView = {
        let image = UIImageView()
        image.contentMode = .scaleAspectFit
        return image
    }()
    private lazy var titleLbl: UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 15, weight: .medium)
        label.textColor = .mediumDarkBlue
        label.numberOfLines = 0
        label.textAlignment = .left
        return label
    }()
    
    lazy var arrowBtn: UIButton = {
        let icon = UIButton()
        icon.setImage(UIImage(named: "arrow"), for: .normal)
        return icon
    }()
    
    init () {
        super.init(frame:.zero)
        self.addUIConstraints()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    private func addUIConstraints () {
        
        self.addSubview(iconImg)
        self.addSubview(titleLbl)
        self.addSubview(arrowBtn)
        snp.makeConstraints { (make) in
            make.height.equalTo(64)
        }
        iconImg.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.leading.equalToSuperview().offset(16)
            make.width.height.equalTo(30)
        }
        titleLbl.snp.makeConstraints { (make) in
            make.leading.equalTo(iconImg.snp.trailing).offset(12)
            make.centerY.equalToSuperview()
            make.trailing.equalToSuperview().offset(-28)
            make.top.bottom.equalToSuperview().inset(25)
        }
        arrowBtn.snp.makeConstraints { (make) in
            make.width.equalTo(8)
            make.height.equalTo(13)
            make.trailing.equalToSuperview().offset(-28)
            make.centerY.equalToSuperview()
        }
    }
    
    public func updateUI (leftIcon: UIImage?, title: String, rightIcon: UIImage? = UIImage(named: "arrow")) {
        self.titleLbl.text = title
        self.iconImg.image = leftIcon
        self.arrowBtn.setImage(rightIcon, for: .normal)
    }
}


final class NavCell: UITableViewCell {
   
    // MARK: - Outlets
        /// Main Container View
    
    private lazy var wrapperView: NavItemView = {
        let view = NavItemView()
        view.clipsToBounds = true
        view.layer.cornerRadius = 10
        view.backgroundColor = .whiteBlueColor
        return view
    }()

    
    
    
    // MARK: - Properties
    
    var navData: NavItem! {
        didSet {
            self.wrapperView.updateUI(leftIcon: navData.icon, title: navData.name ?? "")
        }
    }
    
    // MARK: - UI Methods
    
    private func addUIConstraints () {
        self.contentView.addSubview(wrapperView)
        wrapperView.snp.makeConstraints { (make) in
            make.top.bottom.equalToSuperview().inset(4)
            make.leading.trailing.equalToSuperview()
        }
    }
    
    
    
    // MARK: - Cell Init
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.addUIConstraints()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
